package com.attilax.img;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class imgx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 
		 
		

	}

	public static byte[] toByteArray(File imageFile) throws Exception {
		BufferedImage img = ImageIO.read(imageFile);
		ByteArrayOutputStream buf = new ByteArrayOutputStream((int) imageFile
				.length());
		try {
			ImageIO.write(img, "jpg", buf);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return buf.toByteArray();
	}

	public static byte[] toByteArray(String strUrl) {
		ByteArrayOutputStream baos = null;
		try {
			URL u = new URL(strUrl);
			BufferedImage image = ImageIO.read(u);

			// convert BufferedImage to byte array
			baos = new ByteArrayOutputStream();
			ImageIO.write(image, "jpg", baos);
			baos.flush();

			return baos.toByteArray();
		} catch (Exception e) {
		} finally {
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
				}
			}
		}
		return null;
	}
	
	public static byte[] toByteArray(BufferedImage image,String enc) {
		ByteArrayOutputStream baos = null;
		try {
			//URL u = new URL(strUrl);
		//	BufferedImage image = ImageIO.read(u);

			// convert BufferedImage to byte array
			baos = new ByteArrayOutputStream();
			ImageIO.write(image,enc, baos);
			baos.flush();

			return baos.toByteArray();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
				}
			}
		}
	}

	public static ImageIcon toImg(byte[] by) {
		return  new ImageIcon(by);
	}
	
	
	  public static void setClipboardImage(final Image image)
	    {
	        Transferable trans = new Transferable(){
	            @Override
	            public Object getTransferData(DataFlavor flavor)
	                    throws UnsupportedFlavorException, IOException {
	                // TODO Auto-generated method stub
	                if (isDataFlavorSupported(flavor))
	                {
	                    return image;
	                }                      
	                throw new UnsupportedFlavorException(flavor);
	            }
	 
	            @Override
	            public DataFlavor[] getTransferDataFlavors() {
	                // TODO Auto-generated method stub
	                return new DataFlavor[] { DataFlavor.imageFlavor };
	            }
	 
	            @Override
	            public boolean isDataFlavorSupported(DataFlavor flavor) {
	                // TODO Auto-generated method stub
	                return DataFlavor.imageFlavor.equals(flavor);
	            }             
	        };
	         
	        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
	    }

	public static PixLine getPixLine(BufferedImage image, int lineIndex) {
		int[] pxs=new int[image.getWidth()];
		for(int i=0;i<image.getWidth();i++)
		{
			pxs[i]=	image.getRGB(i, lineIndex);
		}
		PixLine pl=new PixLine();
		pl.pxs=pxs;
		return pl;
	}

	public static boolean isWhiteLine(PixLine pl) {
		int[] pxs=pl.pxs;
		for (int px : pxs) {
			if(px!=-1)
				return false;
		}
		return true;
	}

	public static boolean containsWhiteLine(BufferedImage image) {
		 int heit=image.getHeight();
		 for(int i=0;i<heit;i++)
		 {
			 PixLine pl=getPixLine(image, i);
			 if(isWhiteLine(pl))
				 return true;
		 }
		return false;
	}

}
